package com.thinkTank.oa.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkTank.oa.base.BaseServiceImpl;
import com.thinkTank.oa.dao.RoleMapper;
import com.thinkTank.oa.dao.UserMapper;
import com.thinkTank.oa.entity.User;
import com.thinkTank.oa.service.UserService;
@Transactional
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements  UserService {

	@Autowired
	private RoleMapper roleMapper;
	
	public UserMapper getUserMapper() {
		return (UserMapper)super.getBaseMapper();
	}
	/***
	 * 必须实例化 否则不能调用basedao里面的公用方法
	 * @param userMapper
	 */
	@Autowired	
	public void setUserMapper(UserMapper userMapper) {
		super.setBaseMapper(userMapper);
	}
	@Override
	public User getUserByUsername(String userName) {
		
		return getUserMapper().getUserByUsername(userName);
	}
	@Override
	public Integer addUserAndRole(User user) {
		Integer flag =1;
		flag = this.getUserMapper().add(user);

		String str[] = user.getRole().split(",");
		for(String s:str){
			flag = roleMapper.addUserAndRole(user.getPid(),Integer.parseInt(s));
		}
		
		return flag;
	}
	/***
	 * 根据用户Id查询用户相关信息
	 * @author dq
	 * @date 2018-08-14
	 * @param pid 用户Id
	 * @return
	 */
	@Override
	public User findUserById(String userId) {
		
		return getUserMapper().findUserById(userId);
	}
	
	public Integer updateUserAndRole(User user) {
		Integer flag =1;
		this.getUserMapper().update(user);
		//先删除角色和用户关联表，在插入
		roleMapper.deleteByUserId(user.getPid());
		String str[] = user.getRole().split(",");
		for(String s:str){
			flag = roleMapper.addUserAndRole(user.getPid(),Integer.parseInt(s));
		}
		return flag;
	}
	
	
	@Override
	public List<User> queryInPidList(List<String> pidList) {
		return getUserMapper().queryInPidList(pidList);
	}
	@Override
	public User getUserByFullname(String fullName) {
		return getUserMapper().getUserByFullname(fullName);
	}
	
	@Override
	public List<User> queryInFullname(List<String> fullnameList) {
		return getUserMapper().queryInFullname(fullnameList);
	}
}
